var fieldset_piece_joint = [{
			columnWidth: 0.4,
            xtype: 'fieldset',
            labelWidth: 120,
            title:'piece_joint',
			id:'fieldsetpiece_joint',
            defaults: {width: 140},	// 
            defaultType: 'textfield',
            autoHeight: true,
            bodyStyle: Ext.isIE ? 'padding:0 0 5px 15px;' : 'padding:10px 15px;',
            border: true,
            style: {
                'margin-left': '10px', // when you add custom margin in IE 6...
                'margin-right': Ext.isIE6 ? (Ext.isStrict ? '-10px' : '-13px') : '0'  // you have to adjust for it somewhere else
            },
            items: [
			
					{
						xtype:'textfield',
						fieldLabel: 'id_mail',
						name: 'id_mail',
						id: 'id_mail',
						allowBlank:false
					},
					
					{
						xtype:'textfield',
						fieldLabel: 'piece',
						name: 'piece',
						id: 'piece',
						allowBlank:false
					},
					
					{
						xtype:'textfield',
						fieldLabel: 'description',
						name: 'description',
						id: 'description',
						allowBlank:false
					},
					
				{
					xtype:'hidden',
					name: 'id_piece',
					id: 'id_piece',
					allowBlank:false
				}
				
			]
	}];

		
var colModelpiece_joint =new Ext.grid.ColumnModel([
		{header:'id_mail',  sortable: true,  dataIndex: 'id_mail'},
		{header:'piece',  sortable: true,  dataIndex: 'piece'},
		{header:'description',  sortable: true,  dataIndex: 'description'},
		{id:'id_piece',header: "ID", width:3, sortable: true,  dataIndex: 'id_piece'}
]);

		var grid_piece_joint=[{
				columnWidth: 0.5,
				xtype:'fieldset',
				id:'grid_piece_joint',
				layout: 'fit',
	            items: {
		            xtype: 'grid',
		            ds: dspiece_joint,
		            cm: colModelpiece_joint,
					id:'gridpiece_joint',
		            sm: new Ext.grid.RowSelectionModel({
		                singleSelect: true,
		                listeners: {
		                    rowselect: function(sm, row, rec) {
								Ext.getCmp('formpiece_joint').getForm().loadRecord(rec);
								Ext.getCmp('DELETE').enable();
								Ext.getCmp('UPDATE').enable();
								Ext.getCmp('INSERT').disable();
		                    }
		                }
		            }),
		            autoExpandColumn: 'id_piece',
		            height: 150,
		            title:'liste des piece_joint',
		            border: true,
			        listeners: {
			        	render: function(g) {
			        		g.getSelectionModel().selectRow(0);
			        	},
			        	delay: 10  
			        },
					bbar: new Ext.PagingToolbar({
						pageSize: 20,
						store: dspiece_joint,
						displayInfo: true,
						displayMsg: 'Page actuelle {0} - {1} of {2}',
						emptyMsg: 'Pas de résultat'
					})
	        	}
	        }];
		Ext.onReady(function(){
		    Ext.QuickTips.init(); 
		    Ext.form.Field.prototype.msgTarget = 'side';
		    var bd = Ext.getBody();
			
			function format_todate(val){
				return Ext.util.Format.date(val,'Y-m-d');
			}
		
			dspiece_joint.load(
				{
					waitMsgTarget: true,
					waitMsg:'chargement en cours ...',
					params:{start: 0, limit: 20},
					callback: function(records, options, success){
						if(!success){
							MsgErreurChargement('ERREUR d\'execution de requette de chargement');
						}
					}
				});	
					
		    var gridFormpiece_joint = new Ext.FormPanel({
		        id: 'formpiece_joint',
		        frame: true,
		        labelAlign: 'left',
		        title: 'piece_joint',
		        bodyStyle:'padding:5px',
		        Width: '100%',
		        layout: 'column',	// Specifies that the items will now be arranged in columns
		        renderTo: bd,
				items: [
					grid_piece_joint,
					fieldset_piece_joint 
					
				],buttons:[
						{
            	text: 'Ajouter',
				id:'Ajouter',
		  		handler: function(){
						gridFormpiece_joint.getForm().reset();	
						Ext.getCmp('INSERT').enable();
						Ext.getCmp('DELETE').disable();
						Ext.getCmp('UPDATE').disable();
					}
			},
						{
				 text: 'Inseret',
				 id:'INSERT',
				 disabled:true,
		  		 handler: function(){
		   					if (gridFormpiece_joint.getForm().isValid()) {
								var box = Ext.MessageBox.wait('opération en cours...', 'Mise a jours réuissi');
								
								Ext.Ajax.request({
									url: 'core/frm/action/piece_joint.action.php',
									method: 'POST',
									id: 'update_request',
									params: {
		
										id_mail: Ext.getCmp('id_mail').getValue(),
										piece: Ext.getCmp('piece').getValue(),
										description: Ext.getCmp('description').getValue(),
										id_piece: Ext.getCmp('id_piece').getValue(),

										operation: 'INSERT'
									},
									callback: function(options, success, response){
										var result = Ext.util.JSON.decode(response.responseText);
										
										if(result.success=='true'){
											dspiece_joint.reload();
											Ext.getCmp('grid_piece_joint').doLayout();
											Ext.getCmp('DELETE').disable();
											Ext.getCmp('UPDATE').disable();
											Ext.getCmp('INSERT').disable();
											box.hide();
											DBMsgInfo('');
										}else{
											box.hide();
											DBMsgErreur('');
										}					
									}
								});
							}
							else{
								MsgErreurChamps('');
							}
					}
		},
						{
				 text: 'Supprimer',
				 id:'DELETE',
				 disabled:true,
		  		 handler: function(){
		   					if (gridFormpiece_joint.getForm().isValid()) {
								var box = Ext.MessageBox.wait('opération en cours...', 'Mise a jours réuissi');
								
								Ext.Ajax.request({
									url: 'core/frm/action/piece_joint.action.php',
									method: 'POST',
									id: 'update_request',
									params: {
												id_piece: Ext.getCmp('id_piece').getValue(),

										operation: 'DELETE'
									},
									callback: function(options, success, response){
										var result = Ext.util.JSON.decode(response.responseText);
										
										if(result.success=='true'){
											dspiece_joint.reload();
											Ext.getCmp('grid_piece_joint').doLayout();gridFormpiece_joint.getForm().reset();
											Ext.getCmp('DELETE').disable();
											Ext.getCmp('UPDATE').disable();
											Ext.getCmp('INSERT').disable();
											box.hide();
											DBMsgInfo('');
										}else{
											box.hide();
											DBMsgErreur('');
										}					
									}
								});
							}
							else{
								MsgErreurChamps('');
							}
					}
		},
						{
				 text: 'Mettre à jour',
				 id:'UPDATE',
				 disabled:true,
		  		 handler: function(){
		   					if (gridFormpiece_joint.getForm().isValid()) {
								var box = Ext.MessageBox.wait('opération en cours...', 'Mise a jours réuissi');
								
								Ext.Ajax.request({
									url: 'core/frm/action/piece_joint.action.php',
									method: 'POST',
									id: 'update_request',
									params: {
		
										id_mail: Ext.getCmp('id_mail').getValue(),
										piece: Ext.getCmp('piece').getValue(),
										description: Ext.getCmp('description').getValue(),
										id_piece: Ext.getCmp('id_piece').getValue(),

										operation: 'UPDATE'
									},
									callback: function(options, success, response){
										var result = Ext.util.JSON.decode(response.responseText);
										
										if(result.success=='true'){
											dspiece_joint.reload();
											Ext.getCmp('grid_piece_joint').doLayout();
											Ext.getCmp('DELETE').disable();
											Ext.getCmp('UPDATE').disable();
											Ext.getCmp('INSERT').disable();
											box.hide();
											DBMsgInfo('');
										}else{
											box.hide();
											DBMsgErreur('');
										}					
									}
								});
							}
							else{
								MsgErreurChamps('');
							}
					}
		}
					]
		        		
		    });
		   
		});
